import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QHBoxLayout, QVBoxLayout, QWidget, QTextEdit, QDialog, QCheckBox, QComboBox, QListWidget, QLineEdit, QFileDialog, QLabel
from PyQt5.QtWidgets import QComboBox
from PyQt5.QtCore import Qt
# 用于美化界面的样式表
style_sheet = """
QWidget {
    background-color: #242424;
    color: #fff;
    font-family: Arial, sans-serif;
}
QPushButton {
    min-height: 30px;
    background-color: #3E3E3E;
    color: #fff;
    border: none;
    border-radius: 3px;
    padding: 5px 10px;
    font-size: 14px;
}
QPushButton:hover {
    background-color: #4E4E4E;
}
QTextEdit {
    min-height: 150px;
    border: 1px solid #ddd;
    border-radius: 3px;
    padding: 10px;
    background-color: #333;
    color: #fff;
}
"""

class GetHost:
    # global hosts_list
    # hosts_list = []
    #主机1..临时展示的值
    global hosts_list2
    hosts_list2 = ["主机1", "主机2", "主机3", "主机4"]
    
    """获取主机的方法"""
    def get_hosts(self):
        hosts_list.append("主机1")
        print(hosts_list)
        
        pass
        return hosts_list



class Send:
    def __init__(self) -> None:
        self.file_path = None
    
    def send_message(self, selected_host, message, message_log):
        """
        在这里添加与所选主机的通信逻辑
        """
        print(f"Sending message to {selected_host}: {message}")
        message_log.append(f"To {selected_host}: {message}")
     
    def send_command(self, selected_host, command):
       """
       在这里添加上传执行命令的逻辑
       """
       command_log = []
       print(f"Sending command to {selected_host}: {command}")

       #在此处添加命令执行逻辑
       command_log.append(f"To {selected_host}: {command}")

       #示例 模拟命令执行响应
       response = f"Command {command} executed on {selected_host}"
       self.content_frame.append(response)
    
    def upload(self):
        file_dialog = QFileDialog()
        file_dialog.setFileMode(QFileDialog.ExistingFiles)
        if file_dialog.exec_():
            file_names = file_dialog.selectedFiles()
            return file_names
        return []

    def upload_files(self):
        file_dialog = QFileDialog()
        file_path, _ = file_dialog.getOpenFileName(self.dialog, "上传文件")  # 获取选择的文件路径
        if file_path:
            self.file_path = file_path  # 将文件路径存储在类变量中
            self.file_label.setText(self.file_path)  # 将选择的文件路径显示在标签中
        return file_path
    
    def connect_files(self):
        if self.file_path:
            print(self.file_path)
            """
            在这里执行文件上传的操作
            """
            self.dialog.close()

    def upload_script(self):
        file_names = self.upload()
        for file_name in file_names:
            self.script_list.append(file_name)

    
class MainApplication(QMainWindow, Send, GetHost):
    def __init__(self):
        super().__init__()
        
        self.file_label = QLabel(self)  # 初始化 file_label 属性

        self.setWindowTitle("Server")
        self.setFixedSize(800, 600)

        main_widget = QWidget()
        self.setCentralWidget(main_widget)
        
        #QVBoxLayout垂直布局
        layout = QHBoxLayout()

        # 左侧导航栏
        nav_frame = QWidget()
        nav_layout = QVBoxLayout()

        modules = ["管理主机", "通信", "命令执行", "文件上传", "设置"]
        for module in modules:
            button = QPushButton(module)
            if module == "管理主机":
                button.clicked.connect(self.manage_hosts_window)
            elif module == "通信":
                button.clicked.connect(self.communication_window)
            elif module == "命令执行":
                button.clicked.connect(self.command_execution_window)
            elif module == "文件上传":
                button.clicked.connect(self.upload_file_window)
            else:
                button.clicked.connect(self.settings_window)
            nav_layout.addWidget(button)

        nav_frame.setLayout(nav_layout)
        layout.addWidget(nav_frame)

        # 右侧内容区
        self.content_frame = QTextEdit()
        self.content_frame.setReadOnly(True)
        self.content_frame.setPlaceholderText(str(hosts_list2))
        layout.addWidget(self.content_frame)

        main_widget.setLayout(layout)
        self.setStyleSheet(style_sheet)

        self.script_list = []
        self.setting_options = []
        

    def manage_hosts_window(self):
        dialog = QDialog(self)
        dialog.setWindowTitle("管理主机")

        layout = QVBoxLayout(dialog)
        checkbox_list = []


        add_button = QPushButton("扫描网段", dialog)
        add_button.clicked.connect(lambda: self.scan_host(layout))
        layout.addWidget(add_button)

        delete_button = QPushButton("添加主机", dialog)
        delete_button.clicked.connect(lambda: self.add_host(checkbox_list, layout))
        layout.addWidget(delete_button)

        dialog.setLayout(layout)
        dialog.exec_()

    # 在 add_host 方法中创建新窗口
    def add_host(self, checkbox_list, layout):
        add_dialog = QDialog()
        add_dialog.setWindowTitle("添加主机")
    
        layout = QVBoxLayout(add_dialog)
    
        # 创建输入主机IP的文本框
        host_ip_input = QLineEdit()
        host_ip_input.setPlaceholderText("主机IP")
        layout.addWidget(host_ip_input)
    
        # 创建提交按钮
        submit_button = QPushButton("提交")
        submit_button.clicked.connect(lambda: self.submit_host_info(host_ip_input.text()))  # 调用提交方法
        layout.addWidget(submit_button)
    
        add_dialog.setLayout(layout)
        add_dialog.exec_()
    
    # 在 scan_host 方法中创建新窗口
    def scan_host(self, layout):
        scan_dialog = QDialog()
        scan_dialog.setWindowTitle("扫描设置")
    
        layout = QVBoxLayout(scan_dialog)
    
        # 创建添加网段的文本框
        network_address_input = QLineEdit()
        network_address_input.setPlaceholderText("添加网段")
        layout.addWidget(network_address_input)
    
        # 创建子网掩码的文本框
        subnet_mask_input = QLineEdit()
        subnet_mask_input.setPlaceholderText("子网掩码")
        layout.addWidget(subnet_mask_input)
    
        # 创建提交按钮
        submit_button = QPushButton("提交")
        submit_button.clicked.connect(lambda: self.submit_scan_info(network_address_input.text(), subnet_mask_input.text()))  # 调用提交方法
        layout.addWidget(submit_button)
    
        scan_dialog.setLayout(layout)
        scan_dialog.exec_()
    
    # 提交方法示例
    def submit_host_info(self, host_ip):
        # 在这里实现你的提交逻辑
        print(f"提交主机IP: {host_ip}")
    
    def submit_scan_info(self, network_address, subnet_mask):
        # 在这里实现你的提交逻辑
        print(f"提交网段: {network_address}, 子网掩码: {subnet_mask}")


    def communication_window(self):
        dialog = QDialog(self)
        dialog.setWindowTitle("实时通信")

        layout = QVBoxLayout(dialog)

        host_combo = QComboBox(dialog)
        host_combo.addItems(hosts_list2)  # 将这里的主机列表替换为您的实际主机列表
        layout.addWidget(host_combo)

        message_log = QTextEdit(dialog)
        layout.addWidget(message_log)

        message_entry = QLineEdit(dialog)
        layout.addWidget(message_entry)

        send_button = QPushButton("发送", dialog)
        send_button.clicked.connect(
            lambda: self.send_message(host_combo.currentText(), message_entry.text(), message_log))
        layout.addWidget(send_button)


        dialog.setLayout(layout)
        dialog.exec_()


    def command_execution_window(self):
        dialog = QDialog(self)
        dialog.setWindowTitle("命令执行")

        layout = QVBoxLayout(dialog)
        
        #QComboBox 是下拉列表框组件类
        host_combo = QComboBox(dialog)
        host_combo.addItems(hosts_list2)
        layout.addWidget(host_combo)
        
        #QLineEdit单行文本编辑
        command_entry = QLineEdit(dialog)
        layout.addWidget(command_entry)

        execute_button = QPushButton("执行", dialog)
        
        execute_button.clicked.connect(
            lambda: self.send_command(host_combo.currentText(), command_entry.text()))
        layout.addWidget(execute_button)
        
        """
        这里添加server向client发送命令的动作
        
        """
        layout.addWidget(execute_button)

        dialog.setLayout(layout)
        dialog.exec_()


    def upload_file_window(self):
        self.dialog = QDialog()
        self.dialog.setWindowTitle("文件上传")
        self.dialog.setStyleSheet(style_sheet)  # 将样式表应用于对话框
        layout = QVBoxLayout(self.dialog)

        file_label = QLabel("选择要上传的文件:", self.dialog)
        layout.addWidget(file_label)

        file_list = QListWidget(self.dialog)
        self.file_list = file_list
        layout.addWidget(file_list)

        select_button = QPushButton("选择文件", self.dialog)
        select_button.clicked.connect(self.upload_files)
        layout.addWidget(select_button)

        upload_button = QPushButton("上传", self.dialog)
        upload_button.clicked.connect(self.connect_files)
        layout.addWidget(upload_button)

        self.dialog.setLayout(layout)
        self.dialog.exec_()


    def delete_script(self):
        for item in self.file_list.selectedItems():
            self.script_list.remove(item.text())
            self.file_list.takeItem(self.file_list.row(item))

    def settings_window(self):
        dialog = QDialog(self)
        dialog.setWindowTitle("设置")

        layout = QVBoxLayout(dialog)

        new_option_button = QPushButton("新建", dialog)
        new_option_button.clicked.connect(self.create_option)
        layout.addWidget(new_option_button)

        for option in self.setting_options:
            option_layout = QHBoxLayout()
            option_name = option.split(":")[0]
            button = QPushButton(option_name, dialog)
            option_layout.addWidget(button)

            edit_button = QPushButton("编辑", dialog)
            edit_button.clicked.connect(lambda ch, option_name=option_name: self.edit_option(option_name))
            option_layout.addWidget(edit_button)

            layout.addLayout(option_layout)

        dialog.setLayout(layout)
        dialog.exec_()

    def create_option(self):
        name_dialog = QDialog(self)
        name_dialog.setWindowTitle("脚本上传")
        name_layout = QVBoxLayout(name_dialog)
        name_input = QLineEdit(name_dialog)
        name_layout.addWidget(name_input)

        script_list_widget = QListWidget(name_dialog)
        name_layout.addWidget(script_list_widget)

        upload_button = QPushButton("上传", name_dialog)
        upload_button.clicked.connect(lambda: self.upload_script(name_input.text(), script_list_widget))
        name_layout.addWidget(upload_button)

        save_button = QPushButton("保存", name_dialog)
        save_button.clicked.connect(lambda: self.save_option(name_input, script_list_widget))
        name_layout.addWidget(save_button)

        name_dialog.setLayout(name_layout)
        name_dialog.exec_()

    def upload_script(self, option_name, script_list_widget):
        file_dialog = QFileDialog()
        file_dialog.setFileMode(QFileDialog.ExistingFiles)
        if file_dialog.exec_():
            file_names = file_dialog.selectedFiles()
            for file_name in file_names:
                self.script_list.append((option_name, file_name))
                script_list_widget.addItem(file_name)

    def save_option(self, name_input, script_list_widget):
        name = name_input.text()
        self.setting_options.append(name)
        for script in self.script_list:
            if script[0] == name:
                self.save_script(script[1])
        script_list_widget.clear()

    def save_script(self, script):
        """
        这里实现将上传的脚本保存的地方
        """
        print(f"Saving script: {script}")

    def edit_option(self, option_name):
        edit_dialog = QDialog(self)
        edit_dialog.setWindowTitle("编辑选项")

        layout = QVBoxLayout(edit_dialog)

        edit_input = QLineEdit(edit_dialog)
        edit_input.setText(option_name)
        layout.addWidget(edit_input)

        save_button = QPushButton("保存", edit_dialog)
        save_button.clicked.connect(lambda: self.save_option(edit_input, edit_dialog))
        layout.addWidget(save_button)

        edit_dialog.setLayout(layout)
        edit_dialog.exec_()


if __name__ == '__main__':
    a = GetHost()
    hosts_list = a.get_hosts
    app = QApplication(sys.argv)
    window = MainApplication()
    window.show()
    sys.exit(app.exec_())
